package org.crown.framework.responses;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @author WuFengSheng
 */
@Slf4j
@Data
public class ResponseVo<M> implements Serializable {
    private static final long serialVersionUID = 8100152507762664273L;

    @ApiModelProperty(value = "编码")
    private int code;

    @ApiModelProperty(value = "成功")
    private boolean success;

    @ApiModelProperty(value = "说明")
    private String msg;

    @ApiModelProperty(value = "数据")
    private M data;

    private ResponseVo() {

    }

    /**
     * @return
     */
    public static ResponseVo success() {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(0);
        responseVo.setSuccess(true);
        responseVo.setMsg("ok");
        return responseVo;
    }

    /**
     * @return
     */
    public static ResponseVo success(String msg) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(0);
        responseVo.setSuccess(true);
        responseVo.setMsg(msg);
        return responseVo;
    }

    /**
     * @param m
     * @param <M>
     * @return
     */
    public static <M> ResponseVo success(M m) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(0);
        responseVo.setSuccess(true);
        responseVo.setMsg("ok");
        responseVo.setData(m);
        return responseVo;
    }

    /**
     * @param m
     * @param msg
     * @param <M>
     * @return
     */
    public static <M> ResponseVo success(M m, String msg) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(0);
        responseVo.setSuccess(true);
        responseVo.setMsg(msg);
        responseVo.setData(m);
        return responseVo;
    }

    /**
     * @return
     */
    public static ResponseVo fail() {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(1);
        responseVo.setSuccess(false);
        responseVo.setMsg("fail");
        return responseVo;
    }

    /**
     * @param msg
     * @return
     */
    public static ResponseVo fail(String msg) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(1);
        responseVo.setSuccess(false);
        responseVo.setMsg(msg);
        return responseVo;
    }

    /**
     * @param code
     * @param msg
     * @return
     */
    public static ResponseVo fail(int code, String msg) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(code);
        responseVo.setSuccess(false);
        responseVo.setMsg(msg);
        return responseVo;
    }

    /**
     * @param msg
     * @param data
     * @param <M>
     * @return
     */
    public static <M> ResponseVo fail(String msg, M data) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(1);
        responseVo.setSuccess(false);
        responseVo.setMsg(msg);
        responseVo.setData(data);
        return responseVo;
    }

    /**
     * @param code
     * @param msg
     * @param data
     * @param <M>
     * @return
     */
    public static <M> ResponseVo fail(int code, String msg, M data) {
        ResponseVo responseVo = new ResponseVo();
        responseVo.setCode(code);
        responseVo.setSuccess(false);
        responseVo.setMsg(msg);
        responseVo.setData(data);
        return responseVo;
    }

}
